<?php
class ModelManagerStock extends Model {
	
	//获取业务员全部出库单
	public function getStockIn($filter){

		if(isset($filter['start'])){
			$start = $filter['start'];
		}
		else{
			$start = 0;
		}

		if(isset($filter['limit'])){
			$limit = $filter['limit'];
		}
		else{
			$limit = 10;
		}
		$all_so =M('stock_in')
			->alias('so')
			->join('left join user v on v.user_id=so.user_id')
			->join('left join warehouse w on w.warehouse_id=so.warehouse_id')
			->join('left join refer_type r on r.refer_type_id=so.refer_type_id')
			->join('left join inout_state i on i.id=so.status')
			->join('left join stock_in_detail rd on rd.in_id=so.in_id')
			->distinct('so.in_id')
			->where($where)
			->field('"all" as tab,so.*,v.username, w.name as warehouse_name ,r.name as refer_type,i.name as status_name')
			->order('so.date_added desc')
			->limit($start,$limit)
			->select();
		$where['so.status'] = array('eq',1) ;
		$po_so =M('stock_in')
			->alias('so')
			->join('left join user v on v.user_id=so.user_id')
			->join('left join warehouse w on w.warehouse_id=so.warehouse_id')
			->join('left join refer_type r on r.refer_type_id=so.refer_type_id')
			->join('left join inout_state i on i.id=so.status')
			->join('left join stock_in_detail rd on rd.in_id=so.in_id')
			->distinct('so.in_id')
			->where($where)
			->field('"op" as tab,so.*,v.username, w.name as warehouse_name ,r.name as refer_type,i.name as status_name')
			->order('so.date_added desc')
			->limit($start,$limit)->select();
			// echo M('stock_in')->getlastsql();

		
		$where['so.status'] = array('eq',0) ;
		$cancel_so = M('stock_in')
			->alias('so')
			->join('left join user v on v.user_id=so.user_id')
			->join('left join warehouse w on w.warehouse_id=so.warehouse_id')
			->join('left join refer_type r on r.refer_type_id=so.refer_type_id')
			->join('left join inout_state i on i.id=so.status')
			->join('left join stock_in_detail rd on rd.in_id=so.in_id')
			->distinct('so.in_id')
			->where($where)
			->field('"cancel" as tab,so.*,v.username, w.name as warehouse_name ,r.name as refer_type,i.name as status_name')
			->order('so.date_added desc')
			->limit($start,$limit)
			->select();
			// echo M('stock_in')->getlastsql();
		
			// echo M('stock_in')->getlastsql();
		$so = array_merge($po_so,$cancel_so,$all_so);
		// var_dump($so);
		return $so;


	}
	
	//获取更多单据
	public function loadMoreIn($filter){
		
		if (isset($filter['tab'])){
			$tab = $filter['tab'];
		}

		if(isset($filter['status'])){
			$where['so.status'] = $filter['status'];
		}

		if(isset($filter['start'])){
			$start = $filter['start'];
		}

		if(isset($filter['limit'])){
			$limit = $filter['limit'];
		}
		else{
			$limit = 10;
		}

		$more = M('stock_in')
			->alias('so')
			->join('left join user v on v.user_id=so.user_id')
			->join('left join warehouse w on w.warehouse_id=so.warehouse_id')
			->join('left join refer_type r on r.refer_type_id=so.refer_type_id')
			->join('left join inout_state i on i.id=so.status')
			->join('left join stock_in_detail rd on rd.in_id=so.in_id')
			->distinct('so.in_id')
			->where($where)
			->field('"'.$tab.'"  as tab,so.*,v.username, w.name as warehouse_name ,r.name as refer_type,i.name as status_name')
			->order('so.date_added desc')
			->limit($start,$limit)
			->select();

		return $more;

	}
	
	//获取业务员的电话
	public function getTel($customer_id){

		$tel = M('customer')->where('customer_id='.$customer_id)->getField('telephone');
		return $tel;

	}	
	
	//检查配送
	public function checkSend($data){
			
		$where['out_id'] = $data['out_id'];
		$save['status'] = 3;
		$save['deliver_date'] = date('Y-m-d H:i:s');
		M('stock_in')->where($where)->save($save);

		$so_comment = "手机端更改出库单状态为已配送";
		$this->addStockHistory($data['out_id'],$so_comment);

		$this->checkAllSend($data['refer_id']);

		$message = '单据已配送';

		return $message;

	}
	
	//检查订单的出库单是否全部配送
	public function checkAllSend($refer_id){
		
		$where['so.refer_id'] = $refer_id;
		$where['so.status'] = array('neq',0);
		$isSend = M('stock_in')
			->alias('so')
			->join('`order` o on so.refer_id = o.order_id','left')
			->where($where)
			->group('so.refer_id')
			->field('min(status) as out_status,o.order_status_id as order_status')->select();

		if($isSend[0]['out_status'] > 1 && $isSend[0]['order_status'] == 18){
			
			$user = $this->getUser();

			$order_id = $refer_id;
			$order_status = 3;
			$comment = "用户：".$user[0]['username']."通过手机端配送更改订单状态为待收货";

			$this->load->model('checkout/order');
			$this->model_checkout_order->addOrderHistory($order_id,$order_status,$comment);

		}

	}

	//检查签收
	public function checkSign($data){
			
		$where['out_id'] = $data['out_id'];
		$save['status'] = 4;
		$save['receive_date'] = date('Y-m-d H:i:s');
		M('stock_in')->where($where)->save($save);

		$so_comment = "手机端更改出库单状态为已签收";
		$this->addStockHistory($data['out_id'],$so_comment);

		$this->checkAllSign($data['refer_id']);

		$message = '单据已签收';

		return $message;

	}

	//检查订单的出库单是否全部签收
	public function checkAllSign($refer_id){
		
		$where['so.refer_id'] = $refer_id;
		$where['so.status'] = array('neq',0);
		$isSign = M('stock_in')
			->alias('so')
			->join('`order` o on so.refer_id = o.order_id','left')
			->where($where)
			->group('so.refer_id')
			->field('min(status) as out_status,o.order_status_id as order_status')->select();

		if($isSign[0]['out_status'] > 3 && ($isSign[0]['order_status'] == 18 || $isSign[0]['order_status'] == 3)){
			
			$user = $this->getUser();

			$order_id = $refer_id;
			$order_status = 5;
			$comment = "用户：".$user[0]['username']."通过手机端签收更改订单状态为完成";

			$this->load->model('checkout/order');
			$this->model_checkout_order->addOrderHistory($order_id,$order_status,$comment);

		}

	}
	
	//添加出库单日志
	public function addStockHistory($out_id,$comment){

		$user = $this->getUser();
		
		$data['out_id'] = $out_id;
		$data['user_id'] = $user[0]['user_id'];
		$data['operator_name'] = $user[0]['username'];
		$data['comment'] = $comment;
		$data['notify'] = 0;
		$data['date_added'] = date('Y-m-d H:i:s');

		M('stock_in_history')->data($data)->add();

	}

	//获取用户
	public function getUser(){

		$customer_id = $this->customer->getId();
		$where['contact_tel'] = M('customer')->where('customer_id='.$customer_id)->getField('telephone');
		$user = M('user')->where($where)->select();
		
		return $user;

	}


}
?>